package com.wfh.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.interceptor.RetryOperationsInterceptor;

/**
 * RabbitMQ配置类
 * @Author FengHuan Wang
 * @Date 2025/4/20 8:44
 * @Version 1.0
 */
@Configuration
public class RabbitMQConfig {

    // 交换机名称
    public static final String THUMB_EXCHANGE = "thumb.exchange";
    
    // 队列名称
    public static final String THUMB_QUEUE = "thumb.queue";
    public static final String THUMB_DLQ_QUEUE = "thumb.dlq.queue";
    
    // 路由键
    public static final String THUMB_ROUTING_KEY = "thumb.routing.key";
    public static final String THUMB_DLQ_ROUTING_KEY = "thumb.dlq.routing.key";

    /**
     * 定义交换机
     */
    @Bean
    public DirectExchange thumbExchange() {
        return new DirectExchange(THUMB_EXCHANGE, true, false);
    }

    /**
     * 定义死信交换机
     */
    @Bean
    public DirectExchange thumbDlqExchange() {
        return new DirectExchange("thumb.dlq.exchange", true, false);
    }

    /**
     * 定义主队列
     */
    @Bean
    public Queue thumbQueue() {
        return QueueBuilder.durable(THUMB_QUEUE)
                .withArgument("x-dead-letter-exchange", "thumb.dlq.exchange")
                .withArgument("x-dead-letter-routing-key", THUMB_DLQ_ROUTING_KEY)
                .withArgument("x-message-ttl", 30000) // 30秒TTL
                .build();
    }

    /**
     * 定义死信队列
     */
    @Bean
    public Queue thumbDlqQueue() {
        return QueueBuilder.durable(THUMB_DLQ_QUEUE).build();
    }

    /**
     * 绑定主队列到交换机
     */
    @Bean
    public Binding thumbBinding() {
        return BindingBuilder.bind(thumbQueue())
                .to(thumbExchange())
                .with(THUMB_ROUTING_KEY);
    }

    /**
     * 绑定死信队列到死信交换机
     */
    @Bean
    public Binding thumbDlqBinding() {
        return BindingBuilder.bind(thumbDlqQueue())
                .to(thumbDlqExchange())
                .with(THUMB_DLQ_ROUTING_KEY);
    }

    /**
     * 消息转换器
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 重试拦截器
     */
    @Bean
    public RetryOperationsInterceptor retryInterceptor() {
        return RetryInterceptorBuilder.stateless()
                .maxAttempts(3)
                .backOffOptions(1000, 2.0, 10000)
                .recoverer(new RejectAndDontRequeueRecoverer())
                .build();
    }

    /**
     * 自定义监听器容器工厂
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jsonMessageConverter());
        factory.setAdviceChain(retryInterceptor());
        factory.setConcurrentConsumers(5);
        factory.setMaxConcurrentConsumers(10);
        factory.setPrefetchCount(1000);
        factory.setDefaultRequeueRejected(false);
        return factory;
    }
} 